#ifndef Scheduler_h__
#define Scheduler_h__

#include "ITask.h"
#include "Clock.h"
#include "Common.h"
#include "PriorityQLow.h"


enum TaskType 
{
	TASK_TIME, 
	TASK_FRAME, 
	TASK_RENDER
};
enum TaskState 
{ 
	TASK_ACTIVE,
	TASK_PAUSE, 
	TASK_DELETE 
};

struct TaskInfo
{
	ITask* pTask;
	int status; // 0 = ok, 1 = pause, 2 = delete
	int id;
	void* pUser;

	struct {
		int start;
		int period;
		int duration;

		int last;
		int next;
	} time;
};

#define s_Scheduler Scheduler::Instance()

class Scheduler
{
private:
	PriorityQLow m_taskQueue;
	PriorityQLow m_frameQueue;
	int m_nextId;
	Clock m_clock;

	Scheduler();

public:

	static Scheduler* Instance();
	
	virtual ~Scheduler();

	int IsRunning() { return m_clock.IsRunning(); }
	void Run() { m_clock.Run(); }
	void Stop() { m_clock.Stop(); }

	int DoTimer(int delta);

	void InsertTimeTask(TaskInfo* pTaskInfo);
	void InsertFrameTask(TaskInfo* pTaskInfo);
	void GetNextTimeTask(TaskInfo** ppTaskInfo);
	void GetNextFrameTask(TaskInfo** ppTaskInfo);

	int Schedule(TaskType type, int start, int period, int duration, ITask* pTask, void* pUser, int* pID);

	void Terminate(int id);

	Clock& GetClock()
	{
		return m_clock;
	}
};
#endif // Scheduler_h__